package com.fingard.dsp.bank.directbank.citic01;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;


public class CITIC01Base extends DirectBase {
	protected String getCharset(String p_def, String p_for){
		return getBankConfig().getCharset(p_def, p_for);
	}
	protected void writeCommonHeader(XmlTextWriter xmlWriter, CITIC01Header p_bankHeader) throws Exception{
		/*
<?xml version="1.0" encoding="GBK"?>
<stream>
<action>DLTRNALL</action>
<userName></userName><!--登录名 char(30)-->

		 * */
		if(StringHelper.isNullOrEmpty(p_bankHeader.action))
		{
			throw new Exception("未赋值action");
		}
		xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
		xmlWriter.writeStartElement("stream");
		xmlWriter.writeElementString("action", p_bankHeader.action);
		xmlWriter.writeElementString("userName", bankFront.loginName);
	}
	
	protected String getCharset(){
		return getBankConfig().getCharset("GBK");
	}
	
	protected String[] sendToBank(String p_reqStr) throws Exception{
		
		String[] retStr = new String[]{"",""};
		
		//if(bankFront.org == null || bankFront.org.length() == 0){
		//	throw new Exception("CIS不能为空");
		//}

		WriteBankLogLn("2.提交银行的地址："+bankFront.serverURL);
		WriteBankLogStep2(p_reqStr);

		//发送银行前置
		WebRequest tmpRequest = new WebRequest(bankFront.serverURL);
		String charsetName = getCharset();
		tmpRequest.setConnectTimeout(10000);
		tmpRequest.setRequestProperty("Connection", "close");
		tmpRequest.setRequestProperty("Content-type", "application/xml");
		int readTimeout = getReadTimeout();
		WriteBankLogLn("readTimeout:" + readTimeout/1000 + "秒");
		tmpRequest.setReadTimeout(readTimeout);
		byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
		retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
	}
	//上送格式为json格式
	protected  String[] sendToATS(String jsonData) throws Exception{
		String[] retStr = new String[]{"",""};
		WriteBankLogLn("2.提交ATS的地址："+ FGBiz.dspCenter.dspSet.receiptPushServerUrl);


		//发送银行前置
		WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.receiptPushServerUrl);
		tmpRequest.setConnectTimeout(60000);
		tmpRequest.setRequestProperty("connection", "Close");
		tmpRequest.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		String charsetName = getCharset();
		WriteBankLogLn("3.推送给ATS的json数据"+jsonData);
		byte[] tmpWriteBytes = jsonData.getBytes(charsetName);
		retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
		WriteBankLogLn("4.返回的数据"+retStr[1]);

		return retStr;
	}
	protected static boolean isActiveRetCode(String retCode){
		if(retCode != null){
			if (retCode.equals("AAAAAAA") || retCode.equals("AAAAAAB") || retCode.equals("AAAAAAC") || retCode.equals("AAAAAAE") || retCode.equals("AAAAAAF") || retCode.equals("BBBBBBB") || retCode.equals("CCCCCCC") || retCode.equals("PBRA001")){
				return true;
			}
		}
		return false;
	}
}
